#!/bin/bash

# tree 命令安装脚本
# 适用于 Linux 系统

set -e

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# 输出颜色信息
echo_color() {
    echo -e "${2}${1}${NC}"
}

# 检查依赖
check_dependencies() {
    echo_color "检查系统依赖..." "$YELLOW"
    
    # 检查 g++
    if ! command -v g++ &> /dev/null; then
        echo_color "错误: 未找到 g++ 编译器" "$RED"
        echo_color "请安装 g++:" "$YELLOW"
        echo_color "  Ubuntu/Debian: sudo apt install g++" "$YELLOW"
        echo_color "  CentOS/RHEL: sudo yum install gcc-c++" "$YELLOW"
        echo_color "  Fedora: sudo dnf install gcc-c++" "$YELLOW"
        echo_color "  Arch: sudo pacman -S gcc" "$YELLOW"
        exit 1
    fi
    
    # 检查 C++17 支持
    if ! g++ --version | grep -q "7." && ! g++ --version | grep -q "8." && ! g++ --version | grep -q "9." && ! g++ --version | grep -q "10." && ! g++ --version | grep -q "11." && ! g++ --version | grep -q "12."; then
        echo_color "警告: 建议使用 g++ 7.0 或更高版本以获得更好的 C++17 支持" "$YELLOW"
    fi
    
    echo_color "依赖检查通过" "$GREEN"
}

# 编译程序
compile_tree() {
    echo_color "开始编译 tree 程序..." "$YELLOW"
    
    if [ ! -f "tree.cpp" ]; then
        echo_color "错误: 未找到 tree.cpp 源文件" "$RED"
        echo_color "请确保在 tree.cpp 同一目录下运行此脚本" "$YELLOW"
        exit 1
    fi
    
    # 编译
    g++ -std=c++17 -O2 -o tree tree.cpp
    
    if [ $? -eq 0 ]; then
        echo_color "编译成功" "$GREEN"
    else
        echo_color "编译失败" "$RED"
        exit 1
    fi
}

# 安装到系统路径
install_to_system() {
    echo_color "安装 tree 到系统路径..." "$YELLOW"
    
    # 检查 root 权限
    if [ "$EUID" -ne 0 ]; then
        echo_color "需要 root 权限来安装到系统路径" "$YELLOW"
        echo_color "请输入密码:" "$YELLOW"
        sudo cp tree /usr/local/bin/
        sudo chmod +x /usr/local/bin/tree
    else
        cp tree /usr/local/bin/
        chmod +x /usr/local/bin/tree
    fi
    
    if [ $? -eq 0 ]; then
        echo_color "安装成功！tree 命令现在可以在任何目录使用" "$GREEN"
    else
        echo_color "安装失败" "$RED"
        exit 1
    fi
}

# 安装到用户目录
install_to_user() {
    echo_color "安装 tree 到用户目录..." "$YELLOW"
    
    # 创建用户 bin 目录（如果不存在）
    mkdir -p ~/bin
    
    # 复制文件
    cp tree ~/bin/
    chmod +x ~/bin/tree
    
    # 检查是否在 PATH 中
    if [[ ":$PATH:" != *":$HOME/bin:"* ]]; then
        echo_color "警告: ~/bin 目录不在 PATH 环境变量中" "$YELLOW"
        echo_color "请将以下行添加到 ~/.bashrc 或 ~/.bash_profile:" "$YELLOW"
        echo_color '  export PATH="$HOME/bin:$PATH"' "$YELLOW"
        echo_color "然后运行: source ~/.bashrc" "$YELLOW"
    fi
    
    echo_color "安装成功！tree 命令现在可以在任何目录使用（可能需要重新登录）" "$GREEN"
}

# 显示使用说明
show_usage() {
    echo_color "tree 命令安装完成！" "$GREEN"
    echo ""
    echo_color "使用方法:" "$YELLOW"
    echo_color "  tree                    - 在终端显示当前目录的树状结构" "$NC"
    echo_color "  tree . -o tree.html     - 生成 HTML 格式的目录树" "$NC"
    echo_color "  tree . -o tree.txt      - 生成 TXT 格式的目录树" "$NC"
    echo ""
    echo_color "更多信息请查看 README.md" "$YELLOW"
}

# 主安装流程
main() {
    echo_color "=== tree 命令安装程序 ===" "$GREEN"
    echo ""
    
    # 检查依赖
    check_dependencies
    
    # 编译程序
    compile_tree
    
    # 选择安装方式
    echo ""
    echo_color "请选择安装方式:" "$YELLOW"
    echo_color "1) 系统安装 (需要 root 权限，安装到 /usr/local/bin/)" "$NC"
    echo_color "2) 用户安装 (安装到 ~/bin/)" "$NC"
    echo_color "3) 仅编译，不安装" "$NC"
    echo -n "请输入选择 [1-3]: "
    
    read -r choice
    case $choice in
        1)
            install_to_system
            ;;
        2)
            install_to_user
            ;;
        3)
            echo_color "已编译完成，可执行文件: ./tree" "$GREEN"
            echo_color "可以手动复制到所需位置" "$YELLOW"
            ;;
        *)
            echo_color "无效选择，退出安装" "$RED"
            exit 1
            ;;
    esac
    
    # 显示使用说明
    echo ""
    show_usage
}

# 运行主函数
main
